# Dockerfile for CogVideo
#
# Author: Your Name
# Date: 2025-09-05
#
# --- Base Image ---
# Use an official PyTorch image with CUDA and cuDNN
FROM pytorch/pytorch:2.1.0-cuda12.1-cudnn8-runtime

# --- Metadata ---
LABEL maintainer="Your Name <your.email@example.com>"
LABEL description="CogVideo Inference Server"

# --- Environment Variables ---
ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED=1
ENV PATH="/app/cogvideo_env/bin:$PATH"
ENV HF_HOME="/app/models"
ENV MPLCONFIGDIR="/app/config/matplotlib"

# --- System Dependencies ---
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    git \
    curl \
    ffmpeg \
    libsm6 \
    libxext6 \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# --- Application Setup ---
WORKDIR /app

# Create Python virtual environment
RUN python3 -m venv cogvideo_env

# Copy dependency and config files
COPY requirements.txt config/ ./

# Install Python dependencies
# Ensure execution in the virtual environment
RUN . cogvideo_env/bin/activate && \
    pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# --- Port Exposure ---
EXPOSE 8080

# --- Healthcheck ---
# Add healthcheck for container orchestration tools
HEALTHCHECK --interval=30s --timeout=10s --start-period=300s --retries=3 \
  CMD curl -f http://localhost:8080/health || exit 1

# --- Entrypoint ---
# Use gunicorn to start the service for better performance
CMD ["sh", "-c", " . cogvideo_env/bin/activate && gunicorn cogvideo_server:app -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8080 --timeout 300"]

